%{
#include <stdio.h>
#include "y.tab.h"
extern YYSTYPE yylval;
extern double answ;
int bracketCount = 0;
int first = 0;
int echo = 0;
int hidden = 0;
int lineno = 1;
%}
%option yylineno
%START inSolver inDocument inVar inVarHidden
%%
\\begin\{document\}			{ECHO; BEGIN inDocument;}

<inDocument>\\solver		{BEGIN inSolver; echo = 1; bracketCount = 0; hidden = 0;return SOLVER;}
<inDocument,inSolver>\\answer		{if (echo) printf("%g",answ); return ANSWER;}
<inDocument>\\var			{BEGIN inVar; echo = 0; bracketCount = 0; first = 0; hidden = 1;return VAR;}
<inDocument>\\varsolver		{BEGIN inVar; echo = 0; bracketCount = 0; first = 0; hidden = 0;return VAR_SOLVER;}
<inDocument>\\varhidden		{BEGIN inVar; echo = 0; bracketCount = 0; first = 0; hidden = 1;return VAR_HIDDEN;}
<inDocument>\\hidden		{BEGIN inSolver; echo = 0; bracketCount = 0; hidden = 1;return HIDDEN;}

<inSolver>\+				{if (echo) ECHO; return OP_PLUS; }
<inSolver>\-				{if (echo) ECHO; return OP_MINUS; }
<inSolver>\*				{if (echo) ECHO; return OP_TIMES; }
<inSolver>\\times			{if (echo) ECHO; return OP_TIMES; }
<inSolver>\/				{if (echo) ECHO; return OP_DIV; }
<inSolver>\\div				{if (echo) ECHO; return OP_DIV; }
<inSolver>\(				{if (echo) ECHO; return OP_LPAREN; }
<inSolver>\)				{if (echo) ECHO; return OP_RPAREN; }
<inSolver>\^				{if (echo) ECHO; return OP_EXP; }
<inSolver>!					{if (echo) ECHO; return OP_FACT; }
<inSolver>\|				{if (echo) ECHO; return OP_BAR; }

<inSolver>\\lfloor			{if (echo) ECHO; return OP_LFLOOR;}
<inSolver>\\rfloor			{if (echo) ECHO; return OP_RFLOOR;}
<inSolver>\\lceil			{if (echo) ECHO; return OP_LCEIL;}
<inSolver>\\rceil			{if (echo) ECHO; return OP_RCEIL;}

<inSolver>\\sin				{if (echo) ECHO; return OP_SIN;}
<inSolver>\\cos				{if (echo) ECHO; return OP_COS;}
<inSolver>\\tan				{if (echo) ECHO; return OP_TAN;}

<inSolver>\\arcsin			{if (echo) ECHO; return OP_ARCSIN;}
<inSolver>\\arccos			{if (echo) ECHO; return OP_ARCCOS;}
<inSolver>\\arctan			{if (echo) ECHO; return OP_ARCTAN;}

<inSolver>\\sec				{if (echo) ECHO; return OP_SEC;}
<inSolver>\\csc				{if (echo) ECHO; return OP_CSC;}
<inSolver>\\cot				{if (echo) ECHO; return OP_COT;}

<inSolver>\\sinh			{if (echo) ECHO; return OP_SINH;}
<inSolver>\\cosh			{if (echo) ECHO; return OP_COSH;}
<inSolver>\\tanh			{if (echo) ECHO; return OP_TANH;}
<inSolver>\\sech			{if (echo) ECHO; return OP_SECH;}
<inSolver>\\csch			{if (echo) ECHO; return OP_CSCH;}
<inSolver>\\coth			{if (echo) ECHO; return OP_COTH;}

<inSolver>\\max				{if (echo) ECHO; return OP_MAX;}
<inSolver>\\min				{if (echo) ECHO; return OP_MIN;}

<inSolver>\\p?mod				{if (echo) ECHO; return OP_MOD;}

<inSolver>\\log 		    {if (echo) ECHO; return OP_LOG;}
<inSolver>\\ln       		{if (echo) ECHO; return OP_LN;}

<inSolver>\\sqrt			{if (echo) ECHO; return OP_SQRT;}

<inSolver>\^\\circ			{if (echo) ECHO; return DEGREE;}

<inVar>\{						{bracketCount++; 
									if (first) {
										if (echo) ECHO;
										BEGIN inSolver;
									}
									return L_LBRACE;}
<inVar>\}						{bracketCount--;
									if (!first) {
										first = 1;
										if (!hidden)
											echo = 1;
									}
									return L_RBRACE;}

<inSolver,inVar>[a-zA-Z]	{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}

<inSolver>\\\{					{if (echo) ECHO; return L_VISIBLE_LBRACE;}
<inSolver>\\\}					{if (echo) ECHO; return L_VISIBLE_RBRACE;}
<inSolver>\{					{if (echo) ECHO; bracketCount++; return L_LBRACE; }
<inSolver>\}					{if (echo) ECHO; bracketCount--;
									if (bracketCount == 0) {
										BEGIN inDocument;
										echo = 0;
									}
									return L_RBRACE; }

<inSolver,inVar>_				{if (echo) ECHO; return L_UNDER;	}
<inSolver>\[				 	{if (echo) ECHO; return L_LBRACKET;}
<inSolver>\]					{if (echo) ECHO; return L_RBRACKET; }
<inSolver>\\				{if (echo) ECHO; return L_BACKSLASH; }


<inSolver>,						{if (echo) ECHO; return M_COMMA; }

<inSolver>\\[td]?frac				{if (echo) ECHO; return L_FRAC; }
<inSolver>\\binom				{if (echo) ECHO; return L_BINOM; }


<inSolver,inVar>[0-9]				{if (echo) ECHO; yylval.decimal = atoi(yytext); return DIGIT; }
<inSolver,inVar>\.					{if (echo) ECHO; return DECIMAL; }

\%.*							{ECHO;/*ignore LaTeX comments*/}
\n								{ECHO; lineno++;}
<inSolver>[\t ]				{if (echo) ECHO;}  /*These are all examples of whitespace*/
<inSolver>\\hspace			{if (echo) ECHO;}
<inSolver>\\vspace			{if (echo) ECHO;} 
<inSolver>\\[bB]ig(g)?		{if (echo) ECHO;}
<inSolver>\\[;\\]			{if (echo) ECHO;} 
<inSolver>\\displaystyle	{if (echo) ECHO;}
<inSolver>\\left 			{if (echo) ECHO;}
<inSolver>\\right 			{if (echo) ECHO;}
<inSolver>\\mbox\{[a-zA-Z0-9 \.\?\\,:;|\t\n_+\-*/()&^%#@!\[\]]*\} {if (echo) ECHO;}


<inSolver,inVar>\\alpha				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\[Tt]heta				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\[Uu]psilon			{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\beta				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\vartheta				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\[Pp]i					{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\[Pp]hi				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\[Gg]amma				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\iota				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\varpi					{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\varphi				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\[Dd]elta				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\kappa				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\rho				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\chi				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\epsilon			{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\[Ll]ambda				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\varrho				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\[Pp]si				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\varepsilon			{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\mu					{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\[Ss]igma				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\[Oo]mega				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\Omicron			{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\zeta				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\nu					{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\varsigma				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\eta				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\[Xx]i					{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}
<inSolver,inVar>\\tau				{if (echo) ECHO; yylval.string = (char*)malloc(sizeof(char)*(strlen(yytext)+1)); strcpy(yylval.string,yytext); return VARIABLE;}


<inSolver,inVar>\\[bB]ar				{if (echo) ECHO; yylval.string = yytext; return VAR_MODIFIER;}
<inSolver,inVar>\\[dD]dot				{if (echo) ECHO; yylval.string = yytext; return VAR_MODIFIER;}
<inSolver,inVar>\\[dD]ot				{if (echo) ECHO; yylval.string = yytext; return VAR_MODIFIER;}
<inSolver,inVar>\\[hH]at				{if (echo) ECHO; yylval.string = yytext; return VAR_MODIFIER;}
<inSolver,inVar>\\[vV]ec				{if (echo) ECHO; yylval.string = yytext; return VAR_MODIFIER;}
<inSolver,inVar>\\[cC]heck				{if (echo) ECHO; yylval.string = yytext; return VAR_MODIFIER;}

<<EOF>>							{return T_EOF; }

%%

